﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using FightLand_Sever.Model.Net;
using WebSocketSharp.Server;
using WebSocketSharp;
using System.Net;
using FightLand_Sever.InGame;
using FightLand_Sever.Hall;
using FightLand_Sever.Room;
using System.Threading;

namespace FightLand_Sever
{
    class Player
    {
        static long id = 10001;

        long mark = 0;
        #region 属性
        /// <summary>
        /// 玩家昵称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 玩家IP
        /// </summary>
        public string IP { get; private set; }

        /// <summary>
        /// 玩家唯一ID
        /// </summary>
        public string PlayerID { get; private set; }

        /// <summary>
        /// 剩余金钱
        /// </summary>
        public long Mark
        {
            get
            {
                return this.mark;
            }
            set
            {
                this.mark = value;
            }
        }

        /// <summary>
        /// 性别 0男 1女
        /// </summary>
        public int Gender { get; set; }

        /// <summary>
        /// 返回性别文字
        /// </summary>
        public string Sex { get { return Gender == 0 ? "男" : "女"; } }

        /// <summary>
        /// 人物展示编号
        /// </summary>
        public int RoleID { get; set; }

        /// <summary>
        /// 头像编号
        /// </summary>
        public int HeadID { get; set; }

        /// <summary>
        /// 客户终端信息
        /// </summary>
        public IPEndPoint Ipend { get; private set; }

        /// <summary>
        /// 上一个玩家=>左
        /// </summary>
        public Player LastPlayer { get; set; }

        /// <summary>
        /// 下一个玩家=>右
        /// </summary>
        public Player NextPlayer { get; set; }

        /// <summary>
        /// 包含玩家局内相关数据
        /// </summary>
        public GameInfo GameData { get; private set; }

        /// <summary>
        /// 包含玩家房间内相关数据
        /// </summary>
        public RoomInfo RoomData { get; private set; }

        /// <summary>
        /// 包含玩家大厅相关数据
        /// </summary>
        public HallInfo HallData { get; private set; }
        #endregion

        public Player()
        {
            var ran = new Random();
            //默认信息
            this.Mark = 100000;
            this.PlayerID = id++.ToString();
            this.Name = "玩家" + (id - 10001);
            this.Gender = 0;
            this.HeadID = ran.Next(1, 17);
            this.RoleID = ran.Next(1, 9);
            this.HallData = new HallInfo(this);
            this.RoomData = new RoomInfo(this);
            this.GameData = new GameInfo(this);
        }

        public Player(NetPlayer p, bool newid = false)
        {
            this.Name = p.Name;
            this.PlayerID = newid ? this.PlayerID = id++.ToString() : p.PlayerID;
            this.RoleID = p.RoleID;
            this.Mark = long.Parse(p.Mark);
            this.Gender = p.Gender;
            this.IP = p.IP;
            this.HeadID = int.Parse(p.HeadID);
            this.HallData = new HallInfo(this);
            this.RoomData = new RoomInfo(this);
            this.GameData = new GameInfo(this);
        }

        /// <summary>
        /// 设置游戏对象
        /// </summary>
        /// <param name="game"></param>
        public void SetGame(Game game)
        {
            this.GameData.SetGame(game, this.RoomData.RoomWebsok);
            game.GameEnd += (s, e) =>
            {
                this.GameData = new GameInfo(this);
            };
        }

        /// <summary>
        /// 关闭与玩家的所有WebSok连接
        /// </summary>
        public void ClocseWebSok()
        {
            if (HallData != null && this.HallData.HallWebsok != null)
            {
                this.HallData.HallWebsok.Close();
            }
            if (RoomData != null && this.RoomData.RoomWebsok != null)
            {
                this.RoomData.RoomWebsok.Close();
            }
        }

        public class GameInfo
        {
            public event Action<Player, NetInfoBase> OnGameData; //收到数据触发

            /// <summary>
            /// 获取当前关联的游戏对象
            /// </summary>
            public Game InGame { get; private set; }

            /// <summary>
            /// 获取房间连接对象
            /// </summary>
            public RoomChat GameWebSok { get; private set; }

            /// <summary>
            /// 获取我方玩家
            /// </summary>
            public Player OwnPlayer { get; private set; }

            /// <summary>
            /// 获取或设置玩家手牌
            /// </summary>
            public List<NetPoker> HandPks { get; set; }

            public GameInfo(Player own)
            {
                this.OwnPlayer = own;
            }

            /// <summary>
            /// 设置游戏及连接对象
            /// </summary>
            /// <param name="game"></param>
            /// <param name="chat"></param>
            public void SetGame(Game game, RoomChat chat)
            {
                this.HandPks = new List<NetPoker>();
                this.InGame = game;
                this.GameWebSok = chat;
                chat.OnData += (s, e) =>
                {
                    if (this.OnGameData != null) this.OnGameData(this.OwnPlayer, e);
                };
            }

            /// <summary>
            /// 删除匹配的扑克
            /// </summary>
            public void RemoveHandPk(NetPoker[] pks)
            {
                foreach (var pk in pks)
                {
                    for (int i = 0; i < this.HandPks.Count; i++)
                    {
                        if (pk.Value == this.HandPks[i].Value && pk.Flower == this.HandPks[i].Flower)
                        {
                            HandPks.RemoveAt(i);
                            break;
                        }
                    }
                }
            }

            /// <summary>
            /// 添加扑克手牌并按升序排序
            /// </summary>
            public void AddHandPk(NetPoker[] pks)
            {
                this.HandPks.AddRange(pks);
                var arr = this.HandPks.ToArray();
                this.HandPks.Clear();
                var res = from a in arr orderby a.Value, a.Flower ascending select a;
                this.HandPks.AddRange(res.ToArray());
            }

            public void SendData(string data, GameOrderType order, string tag = "")
            {
                if (this.GameWebSok == null) return;
                this.GameWebSok.SendData(data, (int)order, tag);
            }

            public void SendLastData(string data, GameOrderType order, string tag = "")
            {
                if (this.OwnPlayer.LastPlayer != null)
                {
                    this.OwnPlayer.LastPlayer.GameData.SendData(data, order, tag);
                }
            }

            public void SendNextData(string data, GameOrderType order, string tag = "")
            {
                if (this.OwnPlayer.NextPlayer != null)
                {
                    this.OwnPlayer.NextPlayer.GameData.SendData(data, order, tag);
                }
            }
        }

        public class RoomInfo
        {
            public event Action<Player> RoomSokDisconnect;  //房间连接断开时触发
            public event Action<Player> RoomSokConnect;//房间连接时触发

            /// <summary>
            /// 获取房间内连接对象
            /// </summary>
            public RoomChat RoomWebsok { get; private set; }

            /// <summary>
            /// 获取或设置当前玩家所处的房间对象
            /// </summary>
            public GameRoom RoomWhich { get; set; }

            /// <summary>
            /// 获取或设置该玩家是否是房主
            /// </summary>
            public bool IsRoomMaster { get; set; }

            /// <summary>
            /// 获取或设置玩家是否已准备
            /// </summary>
            public bool OnReady { get; set; }

            /// <summary>
            /// 获取玩家是否已经连接到房间
            /// </summary>
            public bool ConnectRoom { get { return (this.RoomWebsok != null && this.RoomWebsok.IsConnect); } }

            /// <summary>
            /// 获取我方玩家
            /// </summary>
            public Player ownPlayer { get; private set; }

            public RoomInfo(Player own)
            {
                this.ownPlayer = own;
                this.IsRoomMaster = false;
            }

            /// <summary>
            /// 设置连接对象
            /// </summary>
            /// <param name="chat"></param>
            public void SetWebSok(RoomChat chat)
            {
                if (this.RoomWebsok != null) this.RoomWebsok.Close();
                this.RoomWebsok = chat;
                chat.OnDisconnetc += RoomChat_OnClosed;
                if (chat.IsConnect)
                {
                    if (this.RoomSokConnect != null) this.RoomSokConnect(ownPlayer);
                }
            }

            public void SendData(string data, RoomOrderType order, string tag = "")
            {
                if (this.RoomWebsok == null) return;
                this.RoomWebsok.SendData(data, (int)order, tag);
            }

            public void SendLastData(string data, RoomOrderType order, string tag = "")
            {
                if (this.ownPlayer.LastPlayer != null)
                {
                    this.ownPlayer.LastPlayer.RoomData.SendData(data, order, tag);
                }
            }

            public void SendNextData(string data, RoomOrderType order, string tag = "")
            {
                if (this.ownPlayer.NextPlayer != null)
                {
                    this.ownPlayer.NextPlayer.RoomData.SendData(data, order, tag);
                }
            }

            //CallBack->房间连接断开触发
            private void RoomChat_OnClosed(RoomChat sender)
            {
                this.RoomWebsok = null;
                if (RoomSokDisconnect != null)
                {
                    RoomSokDisconnect(ownPlayer);
                }
            }
        }

        public class HallInfo
        {
            public event Action<Player> HallSokDisconnect;  //大厅连接断开时触发 
            public event Action<Player> HallConnect;//大厅连接成功时触发

            /// <summary>
            /// 获取大厅连接对象
            /// </summary>
            public HallChat HallWebsok { get; private set; }

            private Player OwnPlayer;
            public HallInfo(Player own)
            {
                this.OwnPlayer = own;
            }

            /// <summary>
            /// 设置大厅连接对象
            /// </summary>
            /// <param name="chat"></param>
            public void SetWebSock(HallChat chat)
            {
                if (this.HallWebsok != null) this.HallWebsok.Close();
                this.HallWebsok = chat;
                OwnPlayer.IP = chat.IP;
                chat.OnDisConnect += HallChat_OnClosed;
                if (chat.IsConnect)
                {
                    if (this.HallConnect != null) this.HallConnect(this.OwnPlayer);
                }
            }

            /// <summary>
            /// 发送大厅数据
            /// </summary>
            public void SendData(string data, HallOrderType order, string tag = "")
            {
                if (this.HallWebsok != null && this.HallWebsok.IsConnect)
                {
                    this.HallWebsok.SendData(data, order, tag);
                }
            }

            //CallBack->大厅连接断开触发
            private void HallChat_OnClosed(HallChat sender)
            {
                this.HallWebsok = null;
                if (this.HallSokDisconnect != null)
                {
                    this.HallSokDisconnect(OwnPlayer);
                }
            }
        }
    }
}
